package com.ruoyi.kenuo.controller;

import cn.hutool.core.lang.UUID;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.QRCodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author polo Tang
 * @Date 2022-10-24
 */
@RestController
@RequestMapping("/kenuo/qr")
@Api(tags = "二维码接口")
public class QrController {

    /**
     * 1.嵌入二维码的图片路径，要把嵌入二维码的图片提前放到D:/xxffprofile/qrcode/该路径下,
     * 并起名称为qrcode.jpg
     * 2.如果图片名称为空，那么就会生成一个纯净的二维码
     */
    String imgPath = "qrcode.jpg";

    @Value(value = "${ruoyi.httpLu}")
    private String httpLu;


    @ApiOperation(value = "生成二维码",notes = "生成二维码并返回查看路径")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "contents", value = "二维码信息", required = true, dataType = "String")
    })
    @GetMapping("/getQrImage")
    public AjaxResult getQrImage(String saleNel,String snCode, HttpServletResponse response) throws Exception {
        String contents = httpLu + "?snCode="+ snCode + "&saleNel="+saleNel;
        // 生成的二维码的路径
        String destPath = RuoYiConfig.getUploadPath()+"/qrcode/";
        //嵌入图片
        File testFile = new File(destPath+imgPath);
        //目标文件夹
        File filebag = new File(destPath);
        //文件夹是否存在，不存在就创建
        if (!filebag.exists()) {
            filebag.mkdirs();
        }
        String qrImgPath = "";
        //二维码嵌入图片是否存在，不存在就生成纯净的二维码图片
        if(testFile.exists()){
            qrImgPath = destPath+imgPath;
        }
        String newName = System.currentTimeMillis()+".jpg";
        destPath += newName;
        //生成二维码
        QRCodeUtil.encode(contents, qrImgPath, destPath, true);
        String rpath = httpLu+"profile/upload/qrcode/"+newName;
        return  AjaxResult.success(rpath);

    }

}
